package com.house.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.house.entity.House;
import com.house.entity.UserInf;
import com.house.entity.UserLogin;
import com.house.mapper.UserInfMapper;
import com.house.mapper.UserLoginMapper;
import com.house.util.MD5Utils;
import com.house.util.PageRequest;
import com.house.util.PageResult;
import com.house.util.PageUtils;
import com.house.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@Service
public class UserService {
    @Autowired
    private UserLoginMapper userLoginMapper;
    @Autowired
    private UserInfMapper userInfMapper;
    @Autowired
    private MailContentBuilder mailContentBuilder;

    @Autowired
    private MailService mailService;


    public UserLogin login(String admin,String password){
        UserLogin userLogin=userLoginMapper.findByAdminAndPassword(admin, MD5Utils.md5(password));
        return userLogin;
    }

    public int updateUserLogin(UserLogin userLogin){
        return userLoginMapper.updateByPrimaryKey(userLogin);
    }
    public UserLogin findByAdmin(String admin){
        return userLoginMapper.findByAdmin(admin);
    }

    public UserLogin findByEmail(String email){
        return userLoginMapper.findByEmail(email);
    }

    public UserLogin findByTelphone(String telphone){
        return userLoginMapper.findByTelphone(telphone);
    }

    public UserLogin addUserLogin(UserLogin userLogin,String name){
        userLoginMapper.insert(userLogin);
        UserLogin userLogin1=userLoginMapper.findByAdmin(userLogin.getAdmin());
        UserInf userInf=new UserInf();
        userInf.setName(name);
        userInf.setSex("未知");
        //todo:注册照片上传
        userInf.setProfill("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3983189514,3737397695&fm=26&gp=0.jpg");
        userInf.setUserLoginId(userLogin1.getId());
        userInfMapper.insert(userInf);
        userLogin1.setUserInfId(userInfMapper.findByLoginId(userLogin1.getId()).getId());
        userLoginMapper.updateByPrimaryKey(userLogin1);
        return userLogin1;

    }

    public UserLogin findByid(int id){
        return userLoginMapper.selectByPrimaryKey(id);
    }

    public UserInf findByUserLoginId(int loginId){
        return userInfMapper.findByLoginId(loginId);
    }

    public List<UserInf> getAll(){
        return userInfMapper.findAll();
    }

    public PageResult getAll(PageRequest pageRequest){
        return PageUtils.getPageResult(pageRequest, getPageInfo(pageRequest));
    }

    private PageInfo<UserInf> getPageInfo(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<UserInf> users = userInfMapper.findAll();
        return new PageInfo<UserInf>(users);
    }
    public List<UserLogin> findAllByStatus(int status){
        return userLoginMapper.findAllByStatus(status);
    }

    public void updateByIdAndStatus(int id,int status){
        userLoginMapper.updateByIdAndStatus(id,status);
    }

    public void deleteById(int id){
        userLoginMapper.deleteByPrimaryKey(id);
    }

    public void updatePasswordById(int id,String password){

        userLoginMapper.updatePasswordById(id,MD5Utils.md5(password));
    }
    public UserLogin getByEmailorTelphone(String email,String telphone) throws NullPointerException{
        try {
            return userLoginMapper.findByEmail(email);
        }catch (NullPointerException exception){
            return userLoginMapper.findByTelphone(telphone);
        }
    }

    public void updateInf(UserInf userInf){
        userInfMapper.updateByPrimaryKey(userInf);
    }

    public void updateProfillById(String profill,int id){
        userInfMapper.updateProfillById(id,profill);
    }

    public UserInf getByid(int id){
        return userInfMapper.selectByPrimaryKey(id);
    }

    public void insertOneInf(UserInf userInf){
        userInfMapper.insert(userInf);
    }

    public Boolean sendforgetEmail(UserLogin userLogin){
        int seed = new Random().nextInt(20);
        String password = MD5Utils.md5(Integer.toString(seed)).substring(1,6);
        String content = mailContentBuilder.build(userLogin,password);
        try {
            userLogin.setPassword(MD5Utils.md5(password));
            mailService.sendHtmlTemplateMail(userLogin.getEmail(), "恢复密码", content);
            userLoginMapper.updateByPrimaryKey(userLogin);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
